<template>
	<div>
		<el-dialog
			title="角色添加"
			:visible.sync="addChildNoneDialog"
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			:before-close="compileCancel"
		>
			<el-form ref="addForm" :model="addForm" label-width="80px" :rules="rules">
				<el-form-item label="角色名称" prop="roleName">
					<el-input v-model="addForm.roleName"></el-input>
				</el-form-item>
				<el-form-item label="是否启用">
					<el-switch v-model="addForm.addDelivery"></el-switch>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="small" @click="addChild('addForm')"
						>保存</el-button
					>
					<el-button size="small" @click="compileCancel('addForm')"
						>取消</el-button
					>
				</el-form-item>
			</el-form>
		</el-dialog>
	</div>
</template>

<script>
import { roleInsert, roleNameCheck } from '@/http/api.js'
export default {
	name: 'AddChild',
	props: {
		addChildNoneDialog: {
			type: Boolean,
			default: false,
		},
		nodeListData: {
			type: Object,
			default: () => {
				return {}
			},
		},
		roleId: {
			type: String,
			default: () => {
				return
			},
		},
		roleParentUuid: {
			type: String,
			default: () => {
				return
			},
		},
	},
	data() {
		var spCodeValidator = this.spCodeFun
		return {
			addForm: {
				roleName: '',
				addDelivery: false,
			},
			roleDelivery: 0,
			rules: {
				roleName: [
					{ required: true, message: '不能为空', trigger: 'blur' },
					{ validator: spCodeValidator, trigger: 'blur' },
				],
			},
		}
	},

	computed: {
		RowData() {
			return this.nodeListData
		},
	},
	methods: {
		addChild(formName) {
			this.$refs[formName].validate(valid => {
				if (valid) {
					if (this.addForm.addDelivery) {
						this.roleDelivery = 0
					} else {
						this.roleDelivery = 1
					}
					roleInsert({
						roleName: this.addForm.roleName,
						whetherEnable: this.roleDelivery,
						parentUuid: this.roleId,
					}).then(data => {
						if (data.data.head.code == 200) {
							this.$message({
								message: '保存成功',
								type: 'success',
							})
						}
						this.addForm = {
							roleName: '',
							addDelivery: false,
						}
					})
					let dialogValue = {
						verdict: false,
						typeValue: 1,
					}
					this.$emit('addChildClick', dialogValue)
				} else {
					// this.$message.error('保存失败')
					return false
				}
			})
		},

		compileCancel() {
			this.addForm = {
				roleName: '',
				addDelivery: false,
			}
			let dialogValue = {
				verdict: false,
				typeValue: 0,
			}
			this.$emit('addChildClick', dialogValue)
			this.$nextTick(() => {
				this.$refs['addForm'].clearValidate()
			})
		},

		// 查重
		spCodeFun(rule, value, callback) {
			roleNameCheck({
				id: this.roleId,
				parentUuid: this.roleId,
				roleName: value,
			})
				.then(({ data }) => {
					if (data.head.code == 200) {
						callback()
					}
				})
				.catch(({ data }) => {
					if (data.head.code == 400) {
						callback(new Error('账号不能重复'))
						return
					} else {
						callback()
					}
				})
		},
	},
}
</script>

<style></style>
